package com.travel.yang.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.yang.mapper.ProductMapper;
import com.travel.yang.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FavoriteService {
    @Autowired
    private ProductMapper productMapper;

    //查询用户是否收藏线路
    public boolean findFavorite(Integer pid, Integer mid){
        int result = productMapper.findFavoritePidAndMid(pid, mid);
        if(result == 0){
            return false;
        }else {
            return true;
        }
    }

    //收藏
    public void addFavorite(Integer pid,Integer mid){
        if(findFavorite(pid, mid)) {
            // 加唯一标识，比如时间戳+随机数，确认是否被调用多次
            String requestId = System.currentTimeMillis() + "-" + Math.random();
            System.out.println("【Service调用】requestId=" + requestId + ", pid=" + pid + ", mid=" + mid);
            return;
        }
        productMapper.addFavorite(pid,mid);
    }

    //取消收藏
    public void delFavorite(Integer pid, Integer mid){
        productMapper.delFavorite(pid, mid);
    }

    //我的收藏
    public Page<Product> findMemberFavorite(int page, int size, Integer mid){
        Page favoriteProduct = productMapper.findMemberFavorite(new Page(page, size), mid);
        return favoriteProduct;
    }
}
